package com.easyfun.healthmagicbox.inputviews;

import java.sql.SQLException;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.base.BaseActivity;
import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.HealthInputDataUploadTask;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.HealthInputTypeEnum;
import com.easyfun.healthmagicbox.pojo.HealthInputData;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.util.HealthDateUtil;
import com.easyfun.healthmagicbox.widget.DateTimeOnTouchListener;
import com.easyfun.healthmagicbox.widget.SeekBarWithAdjust;
import com.j256.ormlite.dao.Dao;

public class DietInputActivity extends BaseActivity implements DatabaseAccess {

	SeekBarWithAdjust seekbaryaowei;
	TextView txtyaowei;

	SeekBarWithAdjust seekbarxiongwei;
	TextView txtxiongwei;

	SeekBarWithAdjust seekbartunwei;
	TextView txttunwei;

	SeekBarWithAdjust seekbaryinshi;
	TextView txtyinshi;
	SeekBarWithAdjust seekbarsmoking;
	TextView txtsmoking;
	SeekBarWithAdjust seekbarhejiu;
	TextView txthejiuping;

	private SparseArray<String> changedValues = new SparseArray<String>();
	private EditText inputdatetime;
	private static final String LOG_TAG = DietInputActivity.class
			.getSimpleName();

	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputdiet);
		// should after set Content
		addbackButton(this);
		// personal ID
		if (!checkPersonId())
			return;

		seekbaryaowei = (SeekBarWithAdjust) findViewById(R.id.seekbaryaowei);
		txtyaowei = (TextView) findViewById(R.id.txtyaowei);

		seekbarxiongwei = (SeekBarWithAdjust) findViewById(R.id.seekbarxiongwei);
		txtxiongwei = (TextView) findViewById(R.id.txtxiongwei);

		seekbartunwei = (SeekBarWithAdjust) findViewById(R.id.seekbartunwei);
		txttunwei = (TextView) findViewById(R.id.txttunwei);

		seekbaryinshi = (SeekBarWithAdjust) findViewById(R.id.seekbaryinshi);
		txtyinshi = (TextView) findViewById(R.id.txtyinshi);

		seekbarsmoking = (SeekBarWithAdjust) findViewById(R.id.seekbarsmoking);

		txtsmoking = (TextView) findViewById(R.id.txtsmoking);

		seekbarhejiu = (SeekBarWithAdjust) findViewById(R.id.seekbarhejiu);
		txthejiuping = (TextView) findViewById(R.id.txthejiuping);

		Button butSave = (Button) findViewById(R.id.butSaveDiet);
		butSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				saveExamData();
				Toast.makeText(DietInputActivity.this, "保存", Toast.LENGTH_LONG).show();
				
				finish();
			}
		});
		inputdatetime = (EditText) findViewById(R.id.datetimeedit);
		inputdatetime.setText(HealthDateUtil.getCurrentDateStr("yyyy-MM-dd HH:mm"));
		inputdatetime.setOnTouchListener(
				new DateTimeOnTouchListener(this, inputdatetime));
		setupView();
	}

	private void setupView() {
		// -------------------------------------------------------------------------------
		seekbaryaowei.setMax(100); // [40,140]
		seekbaryaowei.setProgress(50);
		txtyaowei.setText(String.valueOf(40 + seekbaryaowei.getProgress())
				+ this.getString(R.string.shengo_limi));

		seekbaryaowei.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.YAOWEI));

		// -------------------------------------------------------------------------------
		seekbarxiongwei.setMax(50); // [70,120]
		seekbarxiongwei.setProgress(15);
		txtxiongwei
				.setText(String.valueOf(70 + seekbarxiongwei.getProgress() * 2)
						+ this.getString(R.string.shengo_limi));

		seekbarxiongwei
				.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
						HealthInputTypeEnum.XIONGWEI));

		// -------------------------------------------------------------------------------
		seekbartunwei.setMax(30); // [70,100]
		seekbartunwei.setProgress(10);
		txttunwei.setText(String.valueOf(70 + seekbartunwei.getProgress())
				+ this.getString(R.string.shengo_limi));

		seekbartunwei.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.TUNWEI));

		// -------------------------------------------------------------------------------
		seekbaryinshi.setMax(1000); // [000,3000] dif 3000
		seekbaryinshi.setProgress(100);
		txtyinshi.setText(String.valueOf(seekbaryinshi.getProgress() * 30)
				+ this.getString(R.string.yinshi_kaluli));

		seekbaryinshi.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.YINSHIKALULI));

		// -------------------------------------------------------------------------------
		seekbarsmoking.setMax(40);
		seekbarsmoking.setProgress(20);
		txtsmoking.setText(String.valueOf(seekbarsmoking.getProgress())
				+ this.getString(R.string.chouyan_gen));

		seekbarsmoking
				.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
						HealthInputTypeEnum.CHOUYAN));

		// -------------------------------------------------------------------------------
		seekbarhejiu.setMax(20);
		seekbarhejiu.setProgress(10);
		txthejiuping.setText(String.valueOf(seekbarhejiu.getProgress())
				+ this.getString(R.string.hejiu_ping));

		seekbarhejiu.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.HEJIU));

	}

	void saveExamData() {
		Dao<HealthInputData, Integer> inputDataDao;
		// get our dao
		try {
			inputDataDao = getHelper(this).getInputDataDao();
			Log.i(LOG_TAG, "get last input data.");
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		// key is the value type which is enum
		int key = 0;
		for (int i = 0; i < changedValues.size(); i++) {
			key = changedValues.keyAt(i); // get the object by the key.
			String value = changedValues.get(key);
			HealthInputData hid = new HealthInputData();
			hid.setPersonID(m_personid);
			long dt = HealthDateUtil.getDate(inputdatetime.getText().toString(),"yyyy-MM-dd HH:mm").getTime();
			hid.setValueTime(dt);// HealthDateUtil.getCurrentDateStr()
			hid.setValueType(key);
			hid.setValueData(value);

			try {
				inputDataDao.create(hid);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				changedValues.clear();
				return;
			}

		}

		HealthInputDataUploadTask task = new HealthInputDataUploadTask(this,
				m_personid, new BasicCommCallback());
		HealthThreadPool.getInstance().execute(task);

		changedValues.clear();

		// close myself equal to go back.
		finish();

	}

	class MyOnSeekBarChangeListener implements OnSeekBarChangeListener {
		private HealthInputTypeEnum mtype;

		public MyOnSeekBarChangeListener(HealthInputTypeEnum type) {
			mtype = type;
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub

			switch (mtype) {
			case YAOWEI:
				changedValues.append(mtype.toInteger(),
						String.valueOf(seekBar.getProgress() + 40));
				txtyaowei.setText(String.valueOf(seekBar.getProgress() + 40)
						+ DietInputActivity.this
								.getString(R.string.shengo_limi));
				break;
			case XIONGWEI:
				changedValues.append(mtype.toInteger(),
						String.valueOf(70 + seekBar.getProgress() * 2));
				txtxiongwei
						.setText(String.valueOf(70 + seekBar.getProgress() * 2)
								+ DietInputActivity.this
										.getString(R.string.shengo_limi));
				break;
			case TUNWEI:
				changedValues.append(mtype.toInteger(),
						String.valueOf(70 + seekBar.getProgress()));
				txttunwei.setText(String.valueOf(70 + seekBar.getProgress())
						+ DietInputActivity.this
								.getString(R.string.shengo_limi));
				break;
			case YINSHIKALULI:
				changedValues.append(mtype.toInteger(),
						String.valueOf( seekBar.getProgress() * 30));
				txtyinshi.setText(String.valueOf(seekBar.getProgress() * 30)
						+ DietInputActivity.this
								.getString(R.string.yinshi_kaluli));
				break;
			case CHOUYAN:
				changedValues.append(mtype.toInteger(),
						String.valueOf(seekBar.getProgress()));
				txtsmoking.setText(String.valueOf(seekBar.getProgress())
						+ DietInputActivity.this
								.getString(R.string.chouyan_gen));
				break;
			case HEJIU:
				changedValues.append(mtype.toInteger(),
						String.valueOf(seekBar.getProgress()));
				txthejiuping
						.setText(String.valueOf(seekBar.getProgress())
								+ DietInputActivity.this
										.getString(R.string.hejiu_ping));
				break;
			default:
				break;
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}
	}
}
